package com.donger.business.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.upms.entity.SysSocialDetails;
import com.donger.business.upms.service.ISysSocialDetailsService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author 7326
 */
@RequestMapping("/sys/social")
@RestController
@AllArgsConstructor
@Api(tags = "社交账号管理开放API")
public class SysSocialDetailsController {

    private final ISysSocialDetailsService sysSocialDetailsService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询社交账号密钥")
    public Result page(Page<SysSocialDetails> page) {
        sysSocialDetailsService.page(page, Wrappers.lambdaQuery());
        return Res.ok(page);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询社交账号密钥详情")
    public Result info(@PathVariable("id") Long id) {
        SysSocialDetails socialDetails = sysSocialDetailsService.getById(id);
        return Res.ok(socialDetails);
    }

    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('sys:social:save')")
    @ApiOperation(value = "新增社交账号密钥")
    public Result save(@RequestBody SysSocialDetails entity) {
        sysSocialDetailsService.save(entity);
        return Res.ok();
    }


    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('sys:social:update')")
    @ApiOperation(value = "更新社交账号密钥")
    public Result update(@RequestBody SysSocialDetails entity) {
        sysSocialDetailsService.updateById(entity);
        return Res.ok();
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除社交账号密钥")
    @PreAuthorize("@pms.hasPermission('sys:social:del')")
    public Result del(@PathVariable("id") Long id) {
        sysSocialDetailsService.removeById(id);
        return Res.ok();
    }


    /**
     * 绑定社交账号
     *
     * @param state 类型
     * @param code  code
     * @return
     */
    @PostMapping("/bind")
    @ApiOperation(value = "当前用户绑定社交账号")
    public Result bindSocial(@ApiParam(value = "社交账号类型") String state, @ApiParam(value = "社交账号code") String code) {
        return Res.ok(sysSocialDetailsService.bindSocial(state, code));
    }
}
